import { useRequest, useAntdTable } from '@/hooks';
import request from '@/utils/request';
import cmdbres from '@/data/cmdbres';
import downloadAxiosResponse from '@/utils/downloadAxiosResponse';

/**
 * 云区域选项
 */
export function useCloudRegionOptions () {
  return useRequest(async () =>
    (await request({ ...cmdbres.queryOssCloudRegions })).data.map(({ cloud_region_name, cloud_region_code }) => ({
      label: cloud_region_name,
      value: cloud_region_code,
    }))
  );
}

/**
 * 任务列表
 */
export function useTaskList (options) {
  console.log(options, 'options');

  return useAntdTable(async (tableData, formData) => {
    console.log(tableData, 'tableData');
    console.log(formData, 'formData');

    const { current, pageSize } = tableData;
    const { cloudRegionCode, status, ossFileUploadTaskName, createTime } = formData;
    console.log(cloudRegionCode, 'cloudRegionCode');
    console.log(status, 'status');
    console.log(ossFileUploadTaskName, 'ossFileUploadTaskName');
    console.log(createTime, 'createTime');

    console.log(cmdbres.queryOssImportTaskList, 'cmdbres.queryOssImportTaskList');


    const { data, total } = await request({
      ...cmdbres.queryOssImportTaskList, //这url 和 请求方式
      data: {
        pageNumber: current,
        pageSize,
        queryFilter: {
          EQ: {
            cloudRegionCode,
            status,
          },
          LIKE: {
            ossFileUploadTaskName: ossFileUploadTaskName || undefined,
          },
        },
        timeConditions: createTime
          ? [
            {
              field: 'createTime',
              start: createTime[0].clone().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
              end: createTime[1].clone().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
            },
          ]
          : [],
      },
    });

    return {
      list: data,
      total,
    };
  }, options);

}

/**
 * 创建任务
 */
export function useCreateTask (options) {
  return useRequest(
    (values) => {
      const { fileList, ...restValues } = values;

      const formData = new FormData();
      formData.append('file', fileList[0]['originFileObj']);
      for (const [k, v] of Object.entries(restValues)) {
        formData.append(k, v);
      }

      return request({ ...cmdbres.createOssImportTask, data: formData });
    },
    {
      manual: true,
      ...options,
    }
  );
}

/**
 * 删除任务
 */
export function useDeleteTask (options) {
  return useRequest((id) => request({ ...cmdbres.deleteOssImportTask(id) }), {
    manual: true,
    ...options,
  });
}

/**
 * 下载OSS导入模板
 */
export function useDownloadOssImportTemplate (options) {
  return useRequest(
    async () =>
      downloadAxiosResponse(
        await request({
          ...cmdbres.downloadOssImportTemplate,
          responseType: 'blob',
        })
      ),
    {
      manual: true,
      ...options,
    }
  );
}

/**
 * 下载资源文件
 */
export function useDownloadResourceFile (options) {
  return useRequest(
    async (id) =>
      downloadAxiosResponse(
        await request({
          ...cmdbres.downloadOssImportData(id),
          responseType: 'blob',
        })
      ),
    {
      manual: true,
      ...options,
    }
  );
}

/**
 * 操作日志列表
 */
export function useOperationLogList (options) {
  return useAntdTable(async (tableData, formData) => {
    const { current, pageSize } = tableData;
    const { resourceName, operator, operatorCode } = formData;

    return (
      await request({
        ...cmdbres.queryOssImportTaskOperationLogList,
        data: {
          page: current,
          pageSize,
          platformCode: 'omp',
          moduleCode: 'CMDB',
          resourceType: 'ImportTaskManagement',
          resourceName: resourceName || undefined,
          operator: operator || undefined,
          operatorCode,
        },
      })
    ).data;
  }, options);
}
